﻿using System;
using System.Collections.Generic; 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using System.Data;
using Runda.CMS.Common.Utility;

namespace Runda.CMS.Portal.Budget
{
    public partial class RptInventoryPutPlan : BasePage
    {
        public string SortString
        {
            get
            {
                if (ViewState["SortString"] == null)
                {
                    ViewState["SortString"] = "customername";
                }

                return ViewState["SortString"].ToString();
            }
            set { ViewState["SortString"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = 2012; i <= 2015; i++)
                {
                    ddlBudgetYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                DataTable dt = new BudgetBll().Companylist(base.CurrentUserID.ToString());
                ddlCompany.DataSource = dt;
                ddlCompany.DataTextField = "Departmentname";
                ddlCompany.DataValueField = "DepartmentCode";
                ddlCompany.DataBind();
                ddlCompany.Items.Add(new ListItem("--不限--", "0"));
                ddlCompany.SelectedValue = "0";

                dt = new BudgetBll().Departmentlist(base.CurrentUserID.ToString());
                ddlDepartment.DataSource = dt;
                ddlDepartment.DataTextField = "Departmentname";
                ddlDepartment.DataValueField = "Departmentname";
                ddlDepartment.DataBind();
                ddlDepartment.Items.Add(new ListItem("--不限--", "0"));
                ddlDepartment.SelectedValue = "0";

                dt = new BudgetBll().Saleslist(base.CurrentUserID.ToString());
                ddlSales.DataSource = dt;
                ddlSales.DataTextField = "username";
                ddlSales.DataValueField = "username";
                ddlSales.DataBind();
                ddlSales.Items.Add(new ListItem("--不限--", "0"));
                ddlSales.SelectedValue = "0";

                dt = new BudgetBll().ProductCategorylist();
                ddlProductCategory.DataSource = dt;
                ddlProductCategory.DataTextField = "categoryName";
                ddlProductCategory.DataValueField = "categoryName";
                ddlProductCategory.DataBind();
                ddlProductCategory.Items.Add(new ListItem("--不限--", "0"));
                ddlProductCategory.SelectedValue = "0";
               
                BindGrid();
               
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
            
           
        }

        protected void gridAccessoryList_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            //if (e.CommandName.ToLower() == "delete")
            //{
            //    BudgetBll bll = new BudgetBll();
            //    bll.DeleteByBudgetID(new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["BudgetID"].ToString()));
            //}
            //BindGrid(AspNetPager1.CurrentPageIndex);

        }

        
        protected void btnRefersh_Click(object sender, EventArgs e)
        {
            BindGrid();
           
        } 

        private void BindGrid()
        {
            string sb = getWhereClause();

            DataTable dt = new BudgetBll().QueryInventoryPutPlan_Rpt(sb, "categoryname");
            if (dt.Rows.Count > 0)
            {
                DataRow drtotal = Calculate().Rows[0];
                DataRow dr = dt.NewRow();
                dr["categoryName"] = "共" + drtotal["categoryName"].ToString() + " 个产品大类";
                dr["productModel"] = drtotal["productModel"].ToString() + " 台仪器";
                dr["m01quantity"] = drtotal["m01quantity"];
                dr["m02quantity"] = drtotal["m02quantity"];
                dr["m03quantity"] = drtotal["m03quantity"];
                dr["m04quantity"] = drtotal["m04quantity"];
                dr["m05quantity"] = drtotal["m05quantity"];
                dr["m06quantity"] = drtotal["m06quantity"];
                dr["m07quantity"] = drtotal["m07quantity"];
                dr["m08quantity"] = drtotal["m08quantity"];
                dr["m09quantity"] = drtotal["m09quantity"];
                dr["m10quantity"] = drtotal["m10quantity"];
                dr["m11quantity"] = drtotal["m11quantity"];
                dr["m12quantity"] = drtotal["m12quantity"];
                dr["m01Amount"] = drtotal["m01Amount"];
                dr["m02Amount"] = drtotal["m02Amount"];
                dr["m03Amount"] = drtotal["m03Amount"];
                dr["m04Amount"] = drtotal["m04Amount"];
                dr["m05Amount"] = drtotal["m05Amount"];
                dr["m06Amount"] = drtotal["m06Amount"];
                dr["m07Amount"] = drtotal["m07Amount"];
                dr["m08Amount"] = drtotal["m08Amount"];
                dr["m09Amount"] = drtotal["m09Amount"];
                dr["m10Amount"] = drtotal["m10Amount"];
                dr["m11Amount"] = drtotal["m11Amount"];
                dr["m12Amount"] = drtotal["m12Amount"];
                dr["Totalquantity"] = drtotal["Totalquantity"];
                dr["TotalPutCost"] = drtotal["TotalPutCost"];

                dt.Rows.Add(dr);
            }

            gridAccessoryList.DataSource = dt;
            gridAccessoryList.DataBind();
        }

        private DataTable Calculate()
        {
            string sb = getWhereClause();

            BudgetBll bll = new BudgetBll();
            DataTable dt = bll.CalcInventoryPutPlan_Rpt(sb);

            return dt;

        }

        private string getWhereClause()
        { 
            StringBuilder sb = new StringBuilder();

            if (base.CurrentUserEntity.UserName == "郭海涛"
                || base.CurrentUserEntity.UserName == "史莹"
                || base.CurrentUserEntity.UserName == "王静"
                || base.CurrentUserEntity.UserName == "顾悦"
                || base.CurrentUserEntity.UserName == "宋欣"
                || base.CurrentUserEntity.UserName == "徐雪峰"
                || base.CurrentUserEntity.UserName == "胡日成"
                || base.CurrentUserEntity.UserName == "崔丽丽"
                || base.CurrentUserEntity.UserName == "官俊"
                || base.CurrentUserEntity.UserName == "李娜"
                || base.CurrentUserEntity.UserName == "张佳锦"
                )
            {
                sb.Append(" and ([company] in(").Append("select DepartmentCode from cms_sys_Department where iscompany = 1 and DepartmentID in(select DepartmentID from Cms_Sys_UserDepartment_DataRight where userid ='").Append(base.CurrentUserID.ToString()).Append("')))");
            }
            else
            {
                sb.Append(" and ([sales] in(").Append("select username from vw_sysUser where DepartmentID in(select DepartmentID from Cms_Sys_UserDepartment_DataRight where userid ='").Append(base.CurrentUserID.ToString()).Append("'))");
                sb.Append(" or [sales] ='").Append(base.CurrentUserEntity.UserName).Append("'");
                sb.Append(" or [Producter] ='").Append(base.CurrentUserEntity.UserName).Append("'");
                sb.Append(" or [ProductManager] ='").Append(base.CurrentUserEntity.UserName).Append("')");
            }

            if (ddlBudgetYear.SelectedValue != string.Empty)
            {
                sb.Append(" and [BudgetYear]  = '").Append(ddlBudgetYear.SelectedValue).Append("'");
            }

            if (ddlCompany.SelectedValue != "0")
            {
                sb.Append(" and company='").Append(ddlCompany.SelectedValue.ToString()).Append("'");
            }

            if (ddlCostType.SelectedValue != "0") { sb.Append(" and costType=").Append(ddlCostType.SelectedValue.ToString()).Append(""); }

            if (this.ddlDepartment.SelectedValue != "0")
            {
                sb.Append(" and sales in(select UserName from vw_sysUser where DepartmentName='").Append(ddlDepartment.SelectedValue.ToString()).Append("')");
            }

            if (ddlSales.SelectedValue != "0")
            {
                sb.Append(" and sales='").Append(ddlSales.SelectedValue.ToString()).Append("'");
            }

            if (this.ddlProductCategory.SelectedValue != "0")
            {
                sb.Append(" and productCategoryCode in(select CategoryCode from Cms_Ctrt_InstrumentCategory where FactoryName='").Append(ddlProductCategory.SelectedValue.ToString()).Append("')");
            }

            return sb.ToString();
        }

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            string sb = getWhereClause();

            DataTable dt = new BudgetBll().ExportInventoryPutPlan_Rpt(sb, "categoryname");

            Export.Export2Excel(dt, "仪器投放预算一览表", Page.Response);

        }

        protected void gridAccessoryList_ItemCreated(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Header)
            {
                //获取表头所在行的所有单元格
                TableCellCollection tcHeader = e.Item.Cells;
                //清除自动生成的表头
                tcHeader.Clear();


                //新添加的第一个表头单元格, 设置为合并17个列, 从而形成一行.
                tcHeader.Add(new TableHeaderCell());
                tcHeader[0].Text = "投放预算一览表";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[1].Text = "投放预算一览表";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[2].ColumnSpan = 29;
                tcHeader[2].Text = "投放预算一览表</th></tr>";
                //</th>表示当前单元格结束, </tr>表示本行结束, <tr>另起新一行    关键点

                //添加第二个表头单元格, 设置为合并两行.
                tcHeader.Add(new TableHeaderCell());
                tcHeader[3].RowSpan = 2;
                tcHeader[3].Text = "产品大类";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[4].RowSpan = 2;
                tcHeader[4].Text = "仪器型号";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[5].RowSpan = 2;
                tcHeader[5].Text = "投放金额";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[6].RowSpan = 2;
                tcHeader[6].Text = "采购单价";


                tcHeader.Add(new TableHeaderCell());
                tcHeader[7].ColumnSpan = 12;
                tcHeader[7].Text = "投放金额";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[8].ColumnSpan = 12;
                tcHeader[8].Text = "投放数量";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[9].RowSpan = 2;
                tcHeader[9].Text = "全年数量合计</th></tr><tr>";


                //第二行的所有的单元格添加完成, 换行</th></tr><tr>

                //添加第三行所有的单元格
                tcHeader.Add(new TableHeaderCell());
                tcHeader[10].Text = "1月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[11].Text = "2月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[12].Text = "3月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[13].Text = "4月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[14].Text = "5月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[15].Text = "6月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[16].Text = "7月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[17].Text = "8月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[18].Text = "9月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[19].Text = "10月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[20].Text = "11月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[21].Text = "12月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[22].Text = "1月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[23].Text = "2月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[24].Text = "3月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[25].Text = "4月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[26].Text = "5月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[27].Text = "6月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[28].Text = "7月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[29].Text = "8月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[30].Text = "9月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[31].Text = "10月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[32].Text = "11月";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[33].Text = "12月</th></tr>";

            }


        }
    }
}
